package com.nehc.nettyserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.nehc.nettyserver.service.UserAuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户认证控制器
 * 提供用户登录、注册、获取信息等功能
 * 
 * @author NEHC
 */
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final UserAuthService userAuthService;
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, String> loginParam) {
        String username = loginParam.get("username");
        String password = loginParam.get("password");
        
        if (username == null || password == null) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "用户名和密码不能为空");
            return ResponseEntity.badRequest().body(result);
        }
        
        Map<String, Object> loginResult = userAuthService.login(username, password);
        
        if ((boolean) loginResult.get("success")) {
            return ResponseEntity.ok(loginResult);
        } else {
            return ResponseEntity.badRequest().body(loginResult);
        }
    }
    
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestBody Map<String, String> registerParam) {
        String username = registerParam.get("username");
        String password = registerParam.get("password");
        String nickname = registerParam.get("nickname");
        
        Map<String, Object> result = new HashMap<>();
        
        if (username == null || password == null) {
            result.put("success", false);
            result.put("message", "用户名和密码不能为空");
            return ResponseEntity.badRequest().body(result);
        }
        
        if (nickname == null) {
            nickname = username;
        }
        
        boolean registerSuccess = userAuthService.register(username, password, nickname);
        
        result.put("success", registerSuccess);
        result.put("message", registerSuccess ? "注册成功" : "注册失败，用户名可能已存在");
        
        return registerSuccess ? ResponseEntity.ok(result) : ResponseEntity.badRequest().body(result);
    }
    
    /**
     * 用户登出
     */
    @PostMapping("/logout")
    public ResponseEntity<Map<String, Object>> logout() {
        Map<String, Object> result = new HashMap<>();
        
        if (StpUtil.isLogin()) {
            StpUtil.logout();
            result.put("success", true);
            result.put("message", "登出成功");
        } else {
            result.put("success", false);
            result.put("message", "用户未登录");
        }
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/info")
    public ResponseEntity<Map<String, Object>> getUserInfo() {
        if (!StpUtil.isLogin()) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "用户未登录");
            return ResponseEntity.status(401).body(result);
        }
        
        Map<String, Object> userInfo = userAuthService.getCurrentUserInfo();
        
        if (userInfo.isEmpty()) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "获取用户信息失败");
            return ResponseEntity.status(500).body(result);
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "获取用户信息成功");
        result.put("user", userInfo);
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 检查登录状态
     */
    @GetMapping("/isLogin")
    public ResponseEntity<Map<String, Object>> isLogin() {
        Map<String, Object> result = new HashMap<>();
        result.put("isLogin", StpUtil.isLogin());
        
        if (StpUtil.isLogin()) {
            result.put("tokenInfo", StpUtil.getTokenInfo());
        }
        
        return ResponseEntity.ok(result);
    }
} 